package report

import (
	"context"
	"sale-admin/internal/app/grpc/client"
	"sale-admin/internal/app/grpc/protoc/system_admin"
	"sale-admin/internal/dal"
	"sale-admin/internal/dao"
	"sale-admin/internal/models"
	"strings"

	"192.168.1.75/go-pkg/errorx"
	"gorm.io/gorm"
)

// 报告配置
type ReportConfigService struct{}

// 新增编辑报告配置
func (rcs ReportConfigService) SaveConfig(ctx context.Context, db *gorm.DB, merchantID, ID string, PutType, RobotSelectType int,
	MerchantSelect, RobotName string, WebHookName int, WebHookAddress, ObjectName string,
) (err error) {
	if PutType < 1 || PutType > 5 {
		return errorx.New("报告类型错误", -1)
	}
	if RobotSelectType == 0 {
		RobotSelectType = 2
	}
	if RobotSelectType != 1 && RobotSelectType != 2 && RobotSelectType != 3 {
		return errorx.New("设备选择类型错误", -1)
	}
	if RobotSelectType == 3 && MerchantSelect == "" {
		return errorx.New("指定商户时请勾选商户", -1)
	}
	if ID == "" {
		err = dao.RobotReportConfig{}.Create(ctx, dal.Q, merchantID, PutType, RobotSelectType,
			MerchantSelect, RobotName, ObjectName, WebHookName, WebHookAddress)
		return err
	} else {
		_, count, _ := dao.RobotReportConfig{}.Detail(ctx, dal.Q, "", ID, 0)
		if count == 0 {
			return errorx.New("配置信息不存在", -1)
		}
		err = dao.RobotReportConfig{}.Update(ctx, dal.Q, merchantID, ID, PutType, RobotSelectType,
			MerchantSelect, RobotName, ObjectName, WebHookName, WebHookAddress)
		return err
	}
}

// 删除
func (rcs ReportConfigService) DelConfig(ctx context.Context, db *gorm.DB, ID string) (err error) {
	_, count, _ := dao.RobotReportConfig{}.Detail(ctx, dal.Q, "", ID, 0)
	if count == 0 {
		return errorx.New("配置信息不存在", -1)
	}
	err = dao.RobotReportConfig{}.Delete(ctx, dal.Q, ID)
	return err
}

// 配置列表
func (rcs ReportConfigService) ConfigList(ctx context.Context, db *gorm.DB, merchantID string, PutType, page, limit int) (
	data []*models.MaRobotConfig, count int64, err error,
) {
	if merchantID == "" {
		err = errorx.New("商户ID不能为空", -1)
		return
	}

	// 处理商户ID
	var merchants []string
	if merchantID != "1" {
		SystemAdminClient, clientErr := client.GetSystemAdminClient()
		if clientErr != nil {
			return
		}
		req := &system_admin.FamilyMerchantsReq{
			MerchantId: merchantID,
		}
		merchantRes, err1 := SystemAdminClient.GetFamilyMerchants(ctx, req)
		if err1 != nil {
			return
		}
		merchants = merchantRes.MerchantIds
	}

	res, count, _ := dao.RobotReportConfig{}.List(ctx, dal.Q, strings.Join(merchants, ","), "", PutType, page, limit)
	for k, v := range res {
		ConfigRobotList := []string{}
		if v.RobotSelectType == 1 { // 本商户所属设备
			SystemAdminClient, clientErr := client.GetSystemAdminClient()
			if clientErr != nil {
				err = clientErr
				return
			}
			// 获取直系商户
			req := &system_admin.FamilyMerchantsReq{
				MerchantId: v.Object,
			}
			rsp, err1 := SystemAdminClient.GetFamilyMerchants(context.Background(), req)
			if err1 != nil {
				return
			}
			robots, robotCount, _ := dao.Robot{}.RobotList(context.Background(), db, strings.Join(rsp.MerchantIds, ","), "", "", "", "", "", "", "")
			if robotCount == 0 {
				continue
			}
			for _, robot := range robots {
				ConfigRobotList = append(ConfigRobotList, robot.RobotName)
			}
			res[k].RobotName = strings.Join(ConfigRobotList, ",")
		} else if v.RobotSelectType == 3 { // 选定商户的设备
			if len(strings.Split(v.MerchantSelect, ",")) == 0 {
				continue
			}
			robots, robotCount, _ := dao.Robot{}.RobotList(context.Background(), db, v.MerchantSelect, "", "", "", "", "", "", "")
			if robotCount == 0 {
				continue
			}
			for _, robot := range robots {
				ConfigRobotList = append(ConfigRobotList, robot.RobotName)
			}
			res[k].RobotName = strings.Join(ConfigRobotList, ",")
		}
	}
	return res, count, nil
}
